﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace AccessControl.Data
{


    public abstract class Entity
    {
        public abstract void ReadValues(DataRow rdr);
        protected abstract void WriteValues(SqlParameterCollection parameters);
        protected abstract bool Insert();
        protected abstract bool Update();

        public bool Exists { get; set; }

        public void ReadValues(SqlDataReader rdr)
        {
            var tbl = new DataTable();
            tbl.Load(rdr);

            if (tbl.Rows.Count > 0)
            {
                ReadValues(tbl.Rows[0]);
                Exists = true;
            }

        }

        public void ReadValues(DataRowView rdr)
        {
            ReadValues(rdr.Row);
        }

        protected static bool FindSingleItem(SqlCommand SQLCmd, Entity instance)
        {
            bool recordFound;
            var Conn = new Connection();
            try
            {
                SQLCmd.Connection = Conn.Connect();

                var rdr = SQLCmd.ExecuteReader();

                instance.ReadValues(rdr);

                recordFound = instance.Exists;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {

                Conn.Disconnect();
            }
            return recordFound;
        }

        public bool SaveData()
        {
            return Exists ? Update() : Exists = Insert();
        }

        public static string GetInsertPart(SqlParameterCollection Params, params string[] ExcludedParams)
        {
            var InsertColumns = new List<string>();
            var InsertValues = new List<string>();
            foreach (SqlParameter param in Params)
            {
                if (!ArrayContains(ExcludedParams, param.ParameterName))
                {
                    InsertColumns.Add(param.ParameterName.Replace("@", ""));
                    InsertValues.Add(param.ParameterName);
                }
            }

            return "(" + string.Join(",",InsertColumns.ToArray()) + ") VALUES (" + string.Join(",", InsertValues.ToArray())+")";
        }
        public static string GetUpdatePart(SqlParameterCollection Params, params string[] ExcludedParams)
        {
            var ParamValues = new List<string>();
            foreach (SqlParameter param in Params)
            {
                if (!ArrayContains(ExcludedParams, param.ParameterName)) ParamValues.Add(param.ParameterName.Replace("@", "") + " = " + param.ParameterName);
            }

            return string.Join(",",ParamValues.ToArray());
        }
        private static bool ArrayContains(string[] StringArray, string value)
        {
            foreach (string item in StringArray) if (item == value) return true;
            return false;
        }

    }

}
